@{
    var isbh = CacheTo.Get(GlobalTo.GetValue("Common:BuildHtmlKey")) as bool? ?? false;
}

<div class="container-fluid">
    <div class="row">

        @if (!isbh)
        {
            <div class="col-md-12 mb-3">
                <a href="/build" target="_blank">构建静态HTML</a>
                <a href="/index.html" class="mx-3" target="_blank">访问静态HTML</a>
            </div>
        }

        <div class="col-md-12">
            <div class="input-group">
                <div class="input-group-prepend">
                    <select class="custom-select" id="seGroup">
                        <option value="">全部</option>
                        @foreach (var gi in NavService.Nav)
                        {
                            <option value="@gi["group"].ToString()">@gi["group"].ToString()</option>
                        }
                    </select>
                </div>
                <div class="input-group-prepend">
                    <button class="btn btn-warning" id="btnSearch">搜索</button>
                </div>
                <input type="text" class="form-control" placeholder="双击空白处 重置搜索条件" id="txtSearch" autocomplete="off">
            </div>
        </div>
    </div>

    <div class="navbox">
        @foreach (var gi in NavService.Nav)
        {
            <div class="row my-3">
                <div class="col-md-12 h5 mt-3"><i class="fa fa-fw text-muted fa-reorder"></i>@gi["group"].ToString()</div>
                @foreach (var item in gi["items"])
                {
                    if (item["display"]?.ToString() == "0")
                    {
                        continue;
                    }

                    var href = item["url"].ToString();
                    if (!isbh && !href.Contains("//"))
                    {
                        href = "/home" + href;
                    }
                    var target = item["target"]?.ToString();
                    if (target != null)
                    {
                        target = "target='" + target + "'";
                    }
                    else if (href.Contains("//"))
                    {
                        target = "target='_blank'";
                    }
                    var title = Html.Raw(item["title"].ToString());
                    var remark = Html.Raw(item["remark"]?.ToString() ?? title.ToString());

                    <div class="col-xxl-2 col-xl-3 col-md-4 col-sm-6 mb-2">
                        <div class="card">
                            <div class="card-body">
                                <a href="@href" @Html.Raw(target)>
                                    <svg><use xlink:href="#@item["icon"].ToString()"></use></svg>
                                    <h5 class="ov">@title</h5>
                                    <small>@remark</small>
                                </a>
                            </div>
                        </div>
                    </div>
                }
            </div>
        }
    </div>
</div>

<script>
    function search(group, keywords) {
        keywords = keywords.trim().toLowerCase();
        var fb = $('.navbox');
        fb.find('a').each(function () {
            var col = $(this).parent().parent().parent();
            if (keywords != "") {
                if ($(this).text().toLowerCase().indexOf(keywords) >= 0 || this.href.toLowerCase().indexOf(keywords) >= 0) {
                    col.removeClass('d-none');
                } else {
                    col.addClass('d-none');
                }
            } else {
                col.removeClass('d-none');
            }
        });
        fb.children().each(function () {
            var cols = $(this).children();
            if (group != "") {
                if (cols.first().text().trim() == group) {
                    $(this).removeClass('d-none');
                } else {
                    $(this).addClass('d-none');
                }
            } else {
                $(this).removeClass('d-none');
                if (cols.filter('.d-none').length + 1 >= cols.length) {
                    cols.first().addClass('d-none');
                } else {
                    cols.first().removeClass('d-none');
                }
            }
        });
    }

    function savels() {
        ss.ls["ss-search"] = $('#txtSearch').val();
        ss.ls["ss-group"] = $('#seGroup').val();
        ss.lsSave();
    }

    var lssearch = ss.lsStr("ss-search") || "",
        lsgroup = ss.lsStr("ss-group") || "";

    $('#seGroup').change(function () {
        savels();
        search($('#seGroup').val(), $('#txtSearch').val())
    }).find('option').each(function () {
        if (this.value == lsgroup) {
            $('#seGroup').val(this.value);
            $('#btnSearch')[0].click();
            return false;
        }
    });

    $('#txtSearch').on('input', function () {
        savels();
        search($('#seGroup').val(), $('#txtSearch').val())
    }).val(lssearch);

    $('#btnSearch').click(function () {
        search($('#seGroup').val(), $('#txtSearch').val())
    })[0].click();

    $(document).dblclick(function () {
        $('#txtSearch').val('');
        $('#seGroup').val('');
        $('#btnSearch')[0].click();
    });

    $('.navbox').find('a').each(function () {
        //兼容本地生成的静态页面
        var na = this;
        if (na.href.indexOf("//localhost") >= 0 && na.href.indexOf("/home/") == -1) {
            na.href += ".html";
        }
    });
</script>